//评论模块路由

var express = require('express')
var router = express.Router()
var { aesDecrypt, checkSession } = require('../untils')
var { moviesModel, commentModel, usersModel } = require('../untils/model')
var moment = require('moment');
const { route } = require('.');
router.get('/test', (req, res) => {
    res.send('test for the comment')
})


router.get('/mdetail', (req, res) => {
    checkSession(req, res, username => {
        var { mid } = req.query;
        moviesModel.findOne({
            id: mid
        }).then(result => {
            res.render('mdetail', {
                username,
                result
            })
        })
    })
})

router.post('/submit', (req, res) => {
    var { mid } = req.query;
    var body = req.body;
    console.log(body);
    checkSession(req, res, username => {
        moviesModel.findOne({
            id: mid
        }).then(movie => {
            body.time = moment().format("YYYY-MM-DD HH:mm:ss");
            body.movie = movie;
            body.author = username;
            body.mid = mid
            commentModel.insertMany(body).then(result => {

                // res.send('评论提交成功');
                res.redirect("/comment/mlist");
            })
        })
    })

})

//评论列表

router.get("/mlist", (req, res) => {
    checkSession(req, res, username => {
        //分页查询
        var page = req.query.page * 1 || 1;
        var pageSize = req.query.pageSize * 1 || 15;
        var total = 0;
        var totalPage = 0;

        commentModel.find({}, {})
            .then(data => {
                total = data.length;
                totalPage = Math.ceil(total / pageSize);

                page = page < 1 ? 1 : page;
                page = page > totalPage ? totalPage : page;

                commentModel.find({}, {})
                    .skip((page - 1) * pageSize)
                    .limit(pageSize)
                    .sort({ _id: -1 })
                    .then(result => {

                        result = result.map((item) => {
                            item = { ...item._doc, time: moment(item.time).format("YYYY-MM-DD HH:mm:ss") };
                            return item;
                        })
                        res.render('mlist', {
                            username,
                            result,
                            page,
                            pageSize,
                            total,
                            totalPage
                        })
                    })
            })





        //查询
        // commentModel.find()
        // .sort({_id:-1})
        // .then(result=>{
        //     result = result.map((item)=>{
        //         item.time = moment(item.time).format("YYYY-MM-DD HH:mm:ss");
        //         console.log(item.time);
        //         return item;
        //     })
        //     res.render('mlist',{username,result})
        // })
    })
})

//评论详情

router.get('/detail', (req, res) => {
    checkSession(req, res, username => {
        const { _id } = req.query;
        commentModel.findOne({
            _id: _id
        }).then(result => {
            result = { ...result._doc, time: moment(result.time).format("YYYY-MM-DD HH:mm:ss") };

            res.render('detail', {
                username,
                result: result.movie,
                data: result
            })
        })
    })
})

//用户的评论列表
router.get("/plist", (req, res) => {
    checkSession(req, res, username => {
        const { author } = req.query;
        commentModel.find({
            author
        }).sort({ _id: -1 })
            .then(result => {
                res.render('plist', {
                    username,
                    author,
                    result
                })
            })
    })
})

//当前评论列表
router.get("/mvdetail", (req, res) => {
    checkSession(req, res, username => {
        const { mid } = req.query;
        commentModel.find({
            mid: mid
        }).sort({ _id: -1 })
            .then(result => {
                result = result.map((item) => {
                    return { ...item._doc, time: moment(item.time).format("YYYY-MM-DD HH:mm:ss") };
                })

                res.render("mvdetail",{
                    username,
                    result,
                    movie:result[0].movie
                })
            })
    })
})


//删除评论
router.post("/delOneComment",(req,res)=>{
    checkSession(req,res,username=>{
        const {_id} = req.body;
        commentModel.remove({
            _id
        }).then(result=>{
            res.json({
                code:200,
                result,
                type:1,
                msg:"删除成功"
            })
        })
    })
})


//修改评论
router.post("/updateOneComment",(req,res)=>{
    checkSession(req,res,username=>{
        const {_id,title,content} = req.body;
        commentModel.updateMany({
            _id
        },{
            $set:{
                title,
                content,
                time:new Date()
            }
        }).then(result=>{
            res.json({
                code:200,
                result,
                type:1,
                msg:"修改成功"
            })
        })
    })
})


module.exports = router;